package com.xiaomi.controller;

import com.xiaomi.domain.Phones;
import com.xiaomi.service.PhonesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

/**
 * @author： YongZ
 * @date： 2024/5/29
 * @description：
 * @modifiedBy：
 */
@Controller
@Slf4j
public class IController {
    @Autowired
    private PhonesService phonesService;

    /*
     * 默认页*/
    @GetMapping("/")
    public String index(Model model, @RequestParam(value = "username", required = false) String username) {
        if (username != null) {
            log.info("用户已登录，欢迎您：" + username);
            model.addAttribute("username", username);
        }
        List<Phones> phonesList = phonesService.getAllPhones();
        model.addAttribute("phoneList", phonesList);
        return "index";
    }



    /*
     * 注册*/
    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    /*
     * 登录*/
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    /*phones 测试接口*/
    @RequestMapping("/phones")
    public List<Phones> phones() {
        List<Phones> phonesList = phonesService.getAllPhones();
        return phonesList;
    }

    /*detail*/
//    @RequestMapping("/detail")
//    public String detail() {
//        return "detail";
//    }

    /*close*/
    @RequestMapping("/close")
    public String close() {
        return "close";
    }
}
